////////////////////////////////////////////////////////////////////////////////////////////////////

//#pragma comment(lib, "Irrlicht.lib")

////////////////////////////////////////////////////////////////////////////////////////////////////

#include "Commons.h"
#include "CGlobals.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "IrrlichtDevice.h"
#include <windows.h>
#include <Mmsystem.h>
#include "crashHandler.h"

#ifdef PFEXPORT
#include "IlluminationMap\PFExporter.h"
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////

CGlobals* g;
CUserInterface* ui;
CLevelEditor* editor;


////////////////////////////////////////////////////////////////////////////////////////////////////
void PrintMemoryStats();

int main()
{	
	

	CrashHandler crash(true);

	g = new CGlobals();

	if (g->init())
		return 1;

	//ui must be created befor the editor
	ui = new CUserInterface();
	
	if (ui->init())			
		return 2;

	editor = new CLevelEditor();
	
	if (editor->init())
		return 3;

	u32 lastTime = g->device->getTimer()->getRealTime();

	while(g->device->run() && g->driver)
	{
		int wait_time = 0;
		u32 t1	=	g->device->getTimer()->getRealTime();
		u32 diff = t1 - lastTime;
		lastTime = t1;

		diff=50;//consistent with game
		
		g->run(diff);		
		ui->update();
		editor->update();
						
		u32 t2	= g->device->getTimer()->getRealTime();
		
		u32 fTime = t2-t1;

		wait_time = ceil ((1000.f / 20.0f) -  (f32)(t2 - t1));

		if(wait_time > 0)
		{		
			g->device->sleep(wait_time , false );				
		}				
	}

	g->close();

	delete ui;
	delete editor;
	delete g;

	PFExporter::FreeMemory();


	#ifdef MEMORY_DEBUG
	PrintMemoryStats();
	#endif //MEMORY_DEBUG

	return 0;

}

////////////////////////////////////////////////////////////////////////////////////////////////////

/*
extern "C" int S_GetTime (void)
{
	return timeGetTime();
}*/

////////////////////////////////////////////////////////////////////////////////////////////////////
